package com.sgu6.sgu6_machine.ui.fragment.buy;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.sgu6.middleware.enums.MachineStatusEnum;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.common.Constant;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.dto.GoodsSimpleDto;
import com.sgu6.sgu6_machine.dto.MemberCardInfoDto;
import com.sgu6.sgu6_machine.dto.MemberCardPayParamsDto;
import com.sgu6.sgu6_machine.event.SelectBuyNoEvent;
import com.sgu6.sgu6_machine.event.SelectTypeEvent;
import com.sgu6.sgu6_machine.presenter.MachineP;
import com.sgu6.sgu6_machine.util.OrderUtils;
import com.sgu6.sgu6_machine.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 读取卡片信息
 */
public class ReadCardF extends BaseF {

    @BindView(R.id.tv_card_number)
    TextView tvCardNumber;
    @BindView(R.id.tv_rice_balance)
    TextView tvRiceBalance;
    @BindView(R.id.tv_water_balance)
    TextView tvWaterBalance;
    @BindView(R.id.tv_pay_money)
    TextView tvPayMoney;

    private GoodsSimpleDto goodsSimpleDto;
    private int buyNo = MachineContainer.machineConfigDto.minBuyNo;
    private MachineP machineP = new MachineP();
    private float bdPay;
    private MemberCardInfoDto memberCardInfoDto;
    private boolean isUserVisible = false;

    public static Fragment newInstance(String params) {
        return new ReadCardF();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_read_card, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            buyNo = 1;
            initViews();
            isUserVisible = false;
        } else {
            isUserVisible = true;
            bdPay = goodsSimpleDto.getPrice() * buyNo;
        }
    }

    private void initViews() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMemberCardInfoEvent(MemberCardInfoDto memberCardInfoDto) {
        Log.i(TAG, "onMemberCardInfoEvent1: " + isUserVisible);
        if (isUserVisible && MachineContainer.isConsumption) {
            bdPay = (float) (Math.round(bdPay * 100)) / 100;
            this.memberCardInfoDto = memberCardInfoDto;
            tvCardNumber.setText(String.format(getResources().getString(R.string.cardNo), memberCardInfoDto.getCardCode()));
            tvRiceBalance.setText(String.format(getResources().getString(R.string.riceBalance), String.valueOf(memberCardInfoDto.getBalance())));
            tvWaterBalance.setText(String.format(getResources().getString(R.string.waterBalance), String.valueOf(memberCardInfoDto.getWaterBalance())));
            tvPayMoney.setText(String.format(getResources().getString(R.string.payMoney), String.valueOf(bdPay)));
            if (memberCardInfoDto.isStatus()) {
                if (MachineContainer.categoryId.equals(Constant.WATER)) {
                    if (memberCardInfoDto.getWaterBalance() >= bdPay) {
                        if (MachineContainer.getMachineStatus() == MachineStatusEnum.待机.code()) {
                            String orderNo = OrderUtils.createOrderNo();
                            machineP.memberCardWaterPay(new MemberCardPayParamsDto(orderNo, MachineContainer.machineConfigDto.machineNo, goodsSimpleDto.getId(), String.valueOf(buyNo), memberCardInfoDto.getMerchantId(), memberCardInfoDto.getCardCode()));
                        } else {
                            ToastUtil.showLongToast(requireContext(), "机器工作中，请稍后重试！");
                        }
                    } else {
                        ToastUtil.showLongToast(requireContext(), "余额不足，请充值");
                    }

                } else {
                    if (memberCardInfoDto.getBalance() >= bdPay) {
                        if (MachineContainer.getMachineStatus() == MachineStatusEnum.待机.code()) {
                            MachineContainer.setMachineStatus(MachineStatusEnum.打米中.code());
                            String orderNo = OrderUtils.createOrderNo();
                            machineP.payByMemberCard(new MemberCardPayParamsDto(orderNo, MachineContainer.machineConfigDto.machineNo, goodsSimpleDto.getId(), String.valueOf(buyNo), memberCardInfoDto.getMerchantId(), memberCardInfoDto.getCardCode()));
                        } else {
                            ToastUtil.showLongToast(requireContext(), "机器工作中，请稍后重试！");
                        }
                    } else {
                        ToastUtil.showLongToast(requireContext(), "余额不足，请充值");
                    }
                }
            }
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelectTypeEvent(SelectTypeEvent event) {
        goodsSimpleDto = event.goodsSimpleDto;
        bdPay = goodsSimpleDto.getPrice() * buyNo;
        tvPayMoney.setText(String.format(getResources().getString(R.string.payMoney), String.valueOf(bdPay)));
        if (event.goodsSimpleDto.getCategoryId().equals(Constant.WATER)){
            buyNo = 5;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBuyNoEvent(SelectBuyNoEvent event) {
        buyNo = event.buyNo;
        bdPay = goodsSimpleDto.getPrice() * buyNo;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

}
